package com.zhengsanming.web.old;

import com.alibaba.fastjson.JSON;
import com.zhengsanming.pojo.User;
import com.zhengsanming.pojo.respdate;
import com.zhengsanming.service.UserServive;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private UserServive servive = new UserServive();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Access-Control-Allow-Origin", "*");
        resp.setHeader("Access-Control-Allow-Credentials", "true");
        req.setCharacterEncoding("utf-8");
        BufferedReader reader = req.getReader();
        String s = reader.readLine();
        User user = JSON.parseObject(s, User.class);
        boolean login = servive.login(user);
        resp.setContentType("text/json;charset=utf-8");
        if (login) {
            resp.setContentType("text/json;charset=utf-8");
            respdate respdate = new respdate();
            respdate.setMsg("注册成功");
            respdate.setStatus("200");
            respdate.setData("abCEFFSDFSDSFSSSDSD-DSADADAUSDASDA");
            String o = JSON.toJSONString(respdate);
            resp.getWriter().write(o);
        } else {
            resp.getWriter().write("用户名或密码错误");
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
